package com.dg11185.nearshop.activity.user.order;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.android.nearshop.R;
import com.dg11185.libs.network.NetClient;
import com.dg11185.nearshop.net.entity.Address;
import com.dg11185.nearshop.view.adapter.AddressAdapter;

/**
 * 
 * @author：Daibo    
 * @time：2014-8-19 上午9:38:39
 */
public class AddressActivity extends Activity{

    private ListView listView = null;
    private AddressAdapter adapter = null;
    private List<Address> addresses = null;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_address);
        
        listView = (ListView) this.findViewById(R.id.address_list);
        adapter = new AddressAdapter(this);
        addresses = new ArrayList<Address>();
        initAddresses();
        adapter.setAddresses(addresses);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        listView.setOnItemClickListener(new ItemClickListener());
    }
    
    @Override
    protected void onDestroy() {
        NetClient.httpCancel(this, true);
        super.onDestroy();
    }
    
    
    public void onBackClicked(View v){
        this.finish();
    }
    
    public void onAddClicked(View v){
        Intent intent = new Intent(this, AddressModifyActivity.class);
        intent.putExtra("tag", AddressModifyActivity.ADD);
        startActivity(intent);
    }
    private class ItemClickListener implements OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(AddressActivity.this, AddressModifyActivity.class);
            intent.putExtra("tag", AddressModifyActivity.MODIFY);
            intent.putExtra("address_entity", addresses.get(position));
            startActivity(intent);
        }
        
    }
    private void initAddresses(){
        for(int a=0; a<3;a++){
            Address address = new Address();
            address.name = "代波";
            address.phone = "13926796732";
            address.province = "广东省";
            address.city = "东莞市";
            address.address = "南城区 三元路6号";
            address.postCode = "523000";
            addresses.add(address);
        }
    }
}
